 ###########################################
#                                           #
# Assert Functions for .ps1 Test Cases      #
#                                           #
# Copyright (C) Microsoft Corporation, 2005 #
#                                           #
 ###########################################

function Get-StackTrace
{
    trap { continue }
    1..100 | %{
        $inv = &{ gv -sc $_ myinvocation } 2>$null
        if ($inv) { write-host -for blue $inv.value.positionmessage.replace("`n","") }
        }
}

# Usage: Assert <bool> <message>
#
function Assert
{
	if ($args.Length -ne 2)
	{
		write-error "Assert takes two parameters."
		#throw
	}

	if (!$args[0]) 
	{
		write-error $args[1]
                Get-StackTrace
		#throw
	}
}

# Usage: AssertEquals <object1> <object2> <message>
#
function AssertEquals
{
	Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertEquals takes either two or three parameters."
	if ($args.Length -eq 2)
	{
		Assert ($args[0] -ceq $args[1]) ("'" + $args[0] + "' does not equal '" + $args[1] + "'")
	}
	else
	{
		Assert ($args[0] -ceq $args[1]) $args[2]
	}
}

# Usage: AssertEqualsCaseInsensitive <object1> <object2> <message>
#
function AssertEqualsCaseInsensitive
{
	Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertEqualsCaseInsensitive takes either two or three parameters."
	if ($args.Length -eq 2)
	{
		Assert ($args[0] -ieq $args[1]) ("'" + $args[0] + "' does not equal '" + $args[1] + "'")
	}
	else
	{
		Assert ($args[0] -ieq $args[1]) $args[2]
	}
}

# Usage: AssertNotEquals <object1> <object2> <message>
#
function AssertNotEquals
{
	Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertNotEquals takes either two or three parameters."
	if ($args.Length -eq 2)
	{
		Assert ($args[0] -cne $args[1]) ("'" + $args[0] + "' equals '" + $args[1] + "'")
	}
	else
	{
		Assert ($args[0] -cne $args[1]) $args[2]
	}
}

# Usage: AssertNotEqualsCaseInsensitive <object1> <object2> <message>
#
function AssertNotEqualsCaseInsensitive
{
	Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertNotEqualsCaseInsensitive takes either two or three parameters."
	if ($args.Length -eq 2)
	{
		Assert ($args[0] -ine $args[1]) ("'" + $args[0] + "' equals '" + $args[1] + "'")
	}
	else
	{
		Assert ($args[0] -ine $args[1]) $args[2]
	}
}

# Usage: AssertNotNull <object> <message>
#
function AssertNotNull
{
	Assert ($args.Length -eq 2) "AssertNotNull takes two parameters."
	Assert ($args[0] -ne $()) $args[1]
}

# Usage: AssertNull <object> <message>
#
function AssertNull
{
	Assert ($args.Length -eq 2) "AssertNull takes two parameters."
	Assert ($args[0] -eq $()) $args[1]
}

# Usage: Fail <message>
#
function Fail
{
	Assert ($args.Length -eq 1) "Fail takes one parameter."
	Assert 0 $args[0]
}